﻿using AutoMapper;
using MediatR;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Errorcode.ErrorCode;
using Management.API.Application.Controller.Management.Product;
using Management.API.DTOs.Management;


namespace Management.API.Application.CommandHandler.Management.Product
{
    public class GetProductTypeCommandHandler:IRequestHandler<GetProductTypeCommand,ApiResult<IList<GetProductTypeDTO>>>
    {
        private readonly IProductTypeRepository iProductTypeRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetProductTypeCommandHandler> _logger;

        public GetProductTypeCommandHandler(IProductTypeRepository IProductTypeRepository,
            IMapper mapper, ILogger<GetProductTypeCommandHandler> logger)
        {
            iProductTypeRepository = IProductTypeRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetProductTypeDTO>>> Handle(GetProductTypeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iProductTypeRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetProductTypeDTO>>(useList);
                return ApiResult<IList<GetProductTypeDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetProductTypeCommandHandler");
                throw;
            }

        }



    }
}
